package com.cz.ssyx.home.service.impl;

import com.cz.ssyx.client.activity.ActivityFeignClient;
import com.cz.ssyx.client.product.ProductFeignClient;
import com.cz.ssyx.client.search.SkuFeignClient;
import com.cz.ssyx.home.service.ItemService;
import com.cz.ssyx.vo.product.SkuInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @descriptions:
 * @author: Joker.
 * @date: 2024/4/30 16:52
 * @version: 1.0
 */

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ActivityFeignClient activityFeignClient;

    @Autowired
    private SkuFeignClient skuFeignClient;

    //查看商品详情
    @Override
    public Map<String, Object> item(Long skuId, Long userId) {
        HashMap<String, Object> map = new HashMap<>();

        //1.查询商品基本信息
        CompletableFuture<Void> skuInfoCompletableFuture = CompletableFuture.runAsync(() -> {
            SkuInfoVo skuInfoVo = productFeignClient.getSkuInfoVoById(skuId);
            map.put("skuInfoVo", skuInfoVo);
        },threadPoolExecutor);

        //2.查询商品的优惠信息
        CompletableFuture<Void> activityCompletableFuture = CompletableFuture.runAsync(() -> {
            Map<String, Object> activityMap = activityFeignClient.findActivityAndCoupon(skuId, userId);  //TODO  远程调用获取商品的活动信息
            map.putAll(activityMap);
        },threadPoolExecutor);

        //3.更新商品热度
        CompletableFuture<Void> hotCompletableFuture = CompletableFuture.runAsync(() -> {
            //TODO 更新热度
            skuFeignClient.incrHotScore(skuId);
        },threadPoolExecutor);

        //4.任务组合
        CompletableFuture.allOf(skuInfoCompletableFuture,
                                activityCompletableFuture,
                                hotCompletableFuture).join();

        return map;
    }
}
